using System.Collections;
using System.Collections.Generic;

namespace Kannon.Core
{
    public class NameValueCollection : ICollection<KeyValuePair<string, string>>
    {
        private readonly IDictionary<string, string> items = new Dictionary<string, string>();

        /// <summary>
        /// Gets or sets an item with the specified <paramref name="name"/>.
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        /// <remarks>
        /// Returns <c>null</c> if an item with the <paramref name="name"/> is not found.
        /// </remarks>
        public string this[string name]
        {
            get { return items.ContainsKey(name) ? items[name] : null; }
            set { items[name] = value; }
        }

        public NameValueCollection()
        {
        }

        public NameValueCollection(IDictionary<string, string> items)
        {
            foreach(var key in items.Keys)
                this.items[key] = items[key];
        }

        public void Add(string key, string value)
        {
            items[key] = value;
        }

        #region ICollection<KeyValuePair<string, string>> Members
        public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
        {
            return items.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public void Add(KeyValuePair<string, string> item)
        {
            items.Add(item);
        }

        public void Clear()
        {
            items.Clear();
        }

        public bool Contains(KeyValuePair<string, string> item)
        {
            return items.Contains(item);
        }

        public void CopyTo(KeyValuePair<string, string>[] array, int arrayIndex)
        {
            items.CopyTo(array, arrayIndex);
        }

        public bool Remove(KeyValuePair<string, string> item)
        {
            return items.Remove(item);
        }

        public int Count
        {
            get { return items.Count; }
        }

        public bool IsReadOnly
        {
            get { return items.IsReadOnly; }
        }
        #endregion
    }
}